package cn.edu.jxau.test;

import java.util.Iterator;

import cn.edu.jxau.util.Queue;

/**
 * 环形链表实现Queue
 * @author 付大石
 */
public class Main {

    public static void main(String[] args) {
        
        CircleLinkedQueue<Integer> queue = new CircleLinkedQueue<>();
        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);
        queue.enqueue(4);
        queue.enqueue(5);
        System.out.println(queue);
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue);
    }

    /**
     * Circle Queue
     * @author 付大石
     */
    private static class CircleLinkedQueue<T> extends Queue<T> {

        private Node<T> last; // last.next就是队列头
        private int size;

        @Override
        public Iterator<T> iterator() {
            throw new UnsupportedOperationException();
        }

        @Override
        public int size() {
            return size;
        }

        @Override
        public void enqueue(T item) {

            Node<T> node = new Node<>(item);
            if (isEmpty()) {
                last = node;
                last.next = node;
            } else {
                node.next = last.next;
                last.next = node;
                last = node;
            }
            size++;
        }

        @Override
        public T dequeue() {

            if (isEmpty()) {
                return null;
            }
            T item = last.next.item;
            if (size > 1) {
                last.next = last.next.next;
            } else {
                last = null;
            }
            size--;
            return item;
        }

        @Override
        public T look() {

            if (isEmpty()) {
                return null;
            }
            return last.next.item;
        }

        @Override
        public String toString() {
            
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("queue size=").append(size).append("\t");
            stringBuilder.append("[");
            if (size == 0) {
                stringBuilder.append("]");
            } else {
                Node<T> cursor = last.next;
                int i = 0;
                while (i<size) {
                    stringBuilder.append(cursor.item).append(",");
                    cursor = cursor.next;
                    i++;
                }
                stringBuilder.deleteCharAt(stringBuilder.length() - 1).append("]");
            }
            return stringBuilder.toString();
        }

        private class Node<T> {
            private T item;
            private Node<T> next;

            public Node() {

            }

            public Node(T item) {
                this.item = item;
            }
        }
    }
}
